package cn.demo.service1.filter;

import cn.demo.service1.entity.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: ljh
 * @Date: 2019/10/10 21:25
 * 请求拦截器
 */
@Component
@Slf4j
public class UserContextFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest)request;
        String corrId=httpServletRequest.getHeader(UserContext.CORRELATION_ID);
        log.info("service1接收的head{}",corrId);
        UserContextHolder.getContext().setCorrelationId(corrId);
        chain.doFilter(httpServletRequest,response);

    }

    @Override
    public void destroy() {

    }
}
